package com.niuniu.log_service.util;

import com.niuniu.log_service.config.RabbitMQConfig;
import com.niuniu.log_service.entity.Log;
import com.niuniu.log_service.mapper.LogMapper;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MQreceiver {
    @Autowired
    LogMapper logMapper;
    @RabbitListener(queues = "log_queue")
    public void receive(List<Message> messages, Channel channel){
        List<Log> logList = messages.stream().map(each-> JsonUtils.parseObject(new String(each.getBody()),Log.class)).collect(Collectors.toList());

        logMapper.addLogs(logList);

    }
}
